package com.hong.strategy.register;

import com.hong.Config.ConfigManager;
import com.hong.Config.RegisterConfig;
import com.hong.RpcServer;
import com.hong.config.constant.ServerConfig;
import com.hong.qrpc.register.nacos.NacosRegister;
import com.hong.qrpc.utils.StringUtils;
import com.hong.strategy.PropertiesStrategy;

import java.util.Map;

/**
 * @Author HQT
 * @Date 2022/7/23 14:56
 */
public class RegisterTypeStrategy implements PropertiesStrategy {
    @Override
    public void disposeProperties(RpcServer rpcServer) {
        ConfigManager configManager = rpcServer.getConfigManager();
        Map<Object, Object> profile = configManager.getProfile();
        String registerType = (String) profile.get(ServerConfig.QRPC_REGISTER_CENTER_TYPE);
        if (!StringUtils.isEmpty(registerType)) {
            RegisterConfig registerConfig = configManager.getRegisterConfig();
            registerConfig.setRegisterType(registerType);
            switch (registerType.toLowerCase()) {
                case "nacos":
                    rpcServer.setRegister(new NacosRegister());
                    return;
                default:
                    throw new IllegalArgumentException("not support properties:qrpc.register.type=" + registerType);
            }
        }
    }
}
